package com.demo.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.demo.bean.Category;
import com.demo.dao.ICategoryDao;
import com.demo.dao.impl.CategoryDaoImpl;
import com.demo.service.ICategoryService;
import com.demo.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class CategoryServiceImpl implements ICategoryService {

	@Autowired
	ICategoryDao dao = new CategoryDaoImpl();
	
	@Override
	public Page<Category> getPage(int index, int limit) {
		Integer count = dao.count();
		
		List<Category> list;
		try {
			if (count == null) {
				throw new SQLException("table data count get null not number");
			}
			
			if (count <= 0) { 
				list = new ArrayList<>();
			} else {
				list = dao.page(index, limit);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		Page<Category> page = new Page<>();
		page.setList(list);
		page.setTotalPage(count / limit + 1);
		return page;
	}

	@Override
	public boolean update(Category category) {
		try {
			return dao.update(category);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean add(Category category) {
		
		try {
			return dao.add(category);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteById(int id) {
		try {
			return dao.delete(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}


}
